package com.hdax.dmw.user.api;

import com.hdax.dmw.common.exceptions.DmException;
import com.hdax.dmw.common.utils.Result;
import com.hdax.dmw.user.dtos.LoginDto;
import com.hdax.dmw.user.service.MsgLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(path = "/login")
public class MsgLoginApi {

    @Autowired
    private MsgLoginService loginService;

    @PostMapping(path = "/msg/login")
    public Result<LoginDto> login(@RequestBody Map<String, String> params){
        String phoneNumber = params.get("phone");
        String vcode = params.get("vcode");
        //
        Result<LoginDto> login = loginService.login(phoneNumber, vcode);
        return login;
    }

    @PostMapping(path = "/msg")
    public Result<String> msg(@RequestBody Map<String, String> params) throws DmException{
        //用户请求手机号
        String phoneNumber = params.get("phone");
        //数据库校验，判断手机号是否已经注册
        return loginService.sendMsg(phoneNumber);
    }
}
